<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <!-- 表格顶部菜单 'delete', 'comSearch', 'quickSearch', 'columnDisplay' -->
        <TableHeader
            :buttons="['refresh', 'add', 'edit']"
            :quick-search-placeholder="t('Quick search placeholder', { fields: t('order.email') + '/' + t('order.mobile') })"
        />

        <!-- 表格 -->
        <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
        <Table ref="tableRef" />

        <!-- 表单 -->
        <PopupForm />
    </div>
</template>

<script setup lang="ts">
import { ref, provide } from 'vue'
import baTableClass from '/@/utils/baTable'
import PopupForm from './popupForm.vue'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'
import { defaultOptButtons } from '/@/components/table'
import { baTableApi } from '/@/api/common'
import { useI18n } from 'vue-i18n'
import { concat, cloneDeep } from 'lodash-es'
import createAxios from '/@/utils/axios'

defineOptions({
    name: 'order',
})

const { t } = useI18n()
const tableRef = ref()
const baTable = new baTableClass(
    new baTableApi('/backend/order/'),
    {
        column: [
            { type: 'selection', align: 'center', operator: false },
            { label: t('Id'), prop: 'id', align: 'center', operator: '=', operatorPlaceholder: t('Id'), width: 70 },
            { label: t('order.order_no'), prop: 'order_no', align: 'center', operator: 'LIKE', operatorPlaceholder: t('Fuzzy query') },
            //{ label: t('order.money'), prop: 'money', align: 'center', operator: 'LIKE', operatorPlaceholder: t('Fuzzy query') },
            { label: t('order.goods_price'), prop: 'money', align: 'center', operator: 'LIKE', operatorPlaceholder: t('Fuzzy query') },
            {
                label: t('order.goods_name'),
                prop: 'uid',
                align: 'center',
                operator: 'LIKE',
                operatorPlaceholder: t('Fuzzy query'),
                width: 190,
                // >= v2.1.2 推荐直接使用 formatter，它是由 el-table-column 自带的，新版系统自定义的单元格渲染器也使用了它格式化
                formatter(row, column, cellValue, index) {
                    //console.log('自定义列显示', row, column, cellValue, index)
                    return row.goods?.goods_name + '（id：'+ cellValue +'）'
                },
            },
            /*{
                label: t('order.goods_number'),
                prop: 'uid',
                align: 'center',
                operator: 'LIKE',
                operatorPlaceholder: t('Fuzzy query'),
                width: 160,
                // >= v2.1.2 推荐直接使用 formatter，它是由 el-table-column 自带的，新版系统自定义的单元格渲染器也使用了它格式化
                formatter(row, column, cellValue, index) {
                    //console.log('自定义列显示', row, column, cellValue, index)
                    return row.goods?.goods_number
                },
            },*/

            { label: t('order.email'), prop: 'email', align: 'center', operator: 'LIKE', operatorPlaceholder: t('Fuzzy query') },
            { label: t('order.card_name'), prop: 'card_name', align: 'center', operator: 'LIKE', operatorPlaceholder: t('Fuzzy query') },
            { label: t('order.card_number'), prop: 'card_number', align: 'center', operator: 'LIKE', operatorPlaceholder: t('Fuzzy query') },

           // { label: t('order.card_date'), prop: 'card_date', align: 'center', operator: 'LIKE', operatorPlaceholder: t('Fuzzy query') },

            { label: t('Create time'), prop: 'ctime', align: 'center', render: 'datetime', sortable: 'custom', operator: 'RANGE', width: 160 },
            {
                label: t('order.status'),
                prop: 'status',
                align: 'center',
                render: 'tag',
                custom: { disable: 'danger', enable: 'success' },
                replaceValue: { 0: t('order.Disable'), 1: t('order.Enable') },
            },
            //{ label: t('State'), prop: 'status', align: 'center', width: '80', render: 'switch' },
            {
                label: t('Operate'),
                align: 'center',
                width: '100',
                render: 'buttons',
                buttons: defaultOptButtons(['edit', 'soft']), // 'delete',
                operator: false,
            },
        ],
        dblClickNotEditColumn: [undefined],
    },
    {
        defaultItems: {
            gender: 0,
            money: '0',
            score: '0',
            status: 'enable',
        },
    },
    {},
    {
        // 切换表单后
        toggleForm({ operate }) {
            if (operate == 'Show') {
                menuRuleTreeUpdate()
            }
            if (operate == 'Edit') {
                menuRuleTreeUpdate()
            }
            if (operate == 'Add') {
                menuRuleTreeUpdate()
            }
        },
        // 编辑请求完成后
        requestEdit() {
            menuRuleTreeUpdate()
        },
        // 编辑请求完成后
        requestShow() {
            menuRuleTreeUpdate()
        },
    }
)

baTable.mount()
baTable.getLists()

provide('baTable', baTable)

const menuRuleTreeUpdate = () => {

    //console.log('弹出修改前', baTable.form.items)
    // 数据来自表格数据,未重新请求api,深克隆,不然可能会影响表格
    //let rowClone = cloneDeep(baTable.form.items)

    // 获取所有的菜单
    // createAxios({
    //         url: '/backend/admin-role/menu',
    //         method: 'get',
    //     }).then((res) => {
    //         baTable.form.extend!.menuRules = res.data
    //         console.log('弹出修改前', baTable.form.items)
    //         console.log(baTable.form.items!.rules)

    //         if (baTable.form.items!.rules && baTable.form.items!.rules.length) {
    //             if (baTable.form.items!.rules.includes('*')) {
    //                 let arr: number[] = []
    //                 for (const key in baTable.form.extend!.menuRules) {
    //                     arr.push(baTable.form.extend!.menuRules[key].id)
    //                 }
    //                 baTable.form.extend!.defaultCheckedKeys = arr
    //             } else {
    //                 baTable.form.extend!.defaultCheckedKeys = baTable.form.items!.rules
    //             }
    //         } else {
    //             baTable.form.extend!.defaultCheckedKeys = []
    //         }

    //         console.log('制作的弹出权限信息', baTable.form.extend!.defaultCheckedKeys)
    //         baTable.form.extend!.treeKey = uuid()
    //     })

    let rowClone = baTable.form.items
    console.log('弹出修改前', rowClone)

    baTable.form.extend!['info'] = rowClone
    //baTable.form.operate = 'edit'
}


</script>

<style scoped lang="scss"></style>
